package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.IOrderSettingDao;
import com.itheima.dao.OrderSetting;
import com.itheima.service.IOrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = IOrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements IOrderSettingService {

    @Autowired
    private IOrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> list) {

        if (list != null){
            for (OrderSetting orderSetting : list) {
                // 判断日期是否存在
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0){ // 已存在,编辑数据
                    orderSettingDao.edit(orderSetting);
                }else {
                    // 不存在,添加数据
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    /**
     * 获取预约数据
     * @param date
     * @return
     */
    @Override
    public List<Map<String, Object>> getOrderSettingByMonth(String date) {

        String begin = date + "-1";
        String end = date + "-31";
        Map<String,String> map = new  HashMap<String,String>();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(map);

        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        // 遍历集合返回符合要求格式
        if (orderSettingList != null){
            for (OrderSetting orderSetting : orderSettingList) {
                Map<String,Object> m = new HashMap<String,Object>();
                m.put("date",orderSetting.getOrderDate().getDate());
                m.put("number",orderSetting.getNumber());
                m.put("reservations",orderSetting.getReservations());
                list.add(m);
            }
        }

        return list;
    }

    /**
     * 设置预约人数
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        orderSettingDao.editNumberByDate(orderSetting);
    }
}
